﻿using Extentions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TrainingReminderSystem.Model;

namespace TrainingReminderSystem.Bll.AutoRun
{
    /// <summary>
    /// 新线程执行
    /// </summary>
    public static class NewThreadRun
    {
        public static Task Task { get; set; }

        /// <summary>
        /// 执行
        /// </summary>
        public static void Start()
        {
            Task = new Task(async () =>
            {
                await Task.Delay(15_000);
                int day = SysConfig.AutoIntervalTime;
                var bll = new SysCustomerBll();
                while (true)
                {
                    using var db = PcLoginedBll.DbNoFilter;
                    var dt = DateTime.Now.AddDays(-day);
                    var list = db.SysCustomer.Where(m => m.CertSearchUpdateTime == null || m.CertSearchUpdateTime <= dt).Take(3).Select(m => m.SysCustomerID).ToList();
                    foreach (var item in list)
                    {
                        var r = bll.Sync(item, db);
                    }
                    if (list.Count > 0)
                    {
                        await Task.Delay(1_000 * 5);
                    }
                    else
                    {
                        await Task.Delay(1_000 * 60 * 3);
                    }
                }
            });
            Task.Start();
        }
    }
}
